package jsyf.oa.web.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.entity.system.CompanyEntity;
import jsyf.oa.facade.system.CompanyFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2017/12/7 0007.
 */
@Api(value = "公司接口", description = "公司接口")
@Controller
@RequestMapping("/company")
public class CompanyController {

    @Resource
    private CompanyFacade companyFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.companyFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        CompanyEntity companyEntity = JSON.toJavaObject(JSON.parseObject(jsonData), CompanyEntity.class);
        String id = companyEntity.getId();
        String result = this.companyFacade.delById(id);
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/sort.json", method = RequestMethod.POST)
    public void sort(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        Boolean result = companyFacade.sort(jsonParam.getString("companyIds"));
        JSONObject objResult = new JSONObject();
        objResult.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/getCompanyList.json", method = RequestMethod.POST)
    public void getCompanyList(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> jsonObjectList = companyFacade.getCompanyList();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(jsonObjectList));
    }

    @ResponseBody
    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "添加公司",
            notes = "添加公司 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "", dataType = "String", paramType = "body")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(this.companyFacade.add(jsonData)));
    }


    @ResponseBody
    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "通过id直接删除公司",
            notes = "通过id直接删除设备 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "id:''", dataType = "String", paramType = "body")
    public void removeById(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = new JSONObject();
        obj.put("isOk", this.companyFacade.removeById(jsonData));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @ResponseBody
    @RequestMapping(value = "/deleteById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "通过id逻辑删除公司",
            notes = "通过id逻辑删除公司 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "id:''", dataType = "String", paramType = "body")
    public void deleteById(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = new JSONObject();
        obj.put("isOk", this.companyFacade.deleteById(jsonData));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @ResponseBody
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "修改公司信息",
            notes = "修改公司信息 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "JSONObject:''", dataType = "String", paramType = "body")
    public void update(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = new JSONObject();
        obj.put("isOk", this.companyFacade.update(jsonData));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }


    @ResponseBody
    @RequestMapping(value = "/updateDevice.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "品牌配置",
            notes = "品牌配置 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "JSONObject:''", dataType = "String", paramType = "body")
    public void updateDevice(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = new JSONObject();
        obj.put("isOk", this.companyFacade.updateDevice(jsonData));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @RequestMapping(value = "/getCompanyListByPage.json", method = RequestMethod.POST)
    public void getCompanyListByPage(HttpServletResponse response, @RequestBody String jsonData) {

        JSONObject records = this.companyFacade.getCompanyListByPage(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }


    @RequestMapping(value = "/getCompanyById.json", method = RequestMethod.POST)
    public void getCompanyById(HttpServletResponse response, @RequestBody String jsonData) {

        JSONObject records = this.companyFacade.getCompanyById(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/getContactAndTelByAddress.json", method = RequestMethod.POST)
    public void getContactAndTelByAddress(HttpServletResponse response, @RequestBody String jsonData) {

        JSONObject records = this.companyFacade.getContactAndTelByAddress(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(""));
        }
    }

    @RequestMapping(value = "/getCompanyListByUserId.json", method = RequestMethod.POST)
    public void getCompanyListByUserId(HttpServletResponse response, @RequestBody String jsonData) {

        List<JSONObject> records = this.companyFacade.getCompanyListByUserId(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrYearTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/getCompanyByUserId.json", method = RequestMethod.POST)
    public void getCompanyByUserId(HttpServletResponse response, @RequestBody String jsonData) {

        List<JSONObject> records = this.companyFacade.getCompanyByUserId(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrYearTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/getBanksByCompanyId.json", method = RequestMethod.POST)
    public void getBanksByCompanyId(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = companyFacade.getBanksByCompanyId(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrYearTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/getBanksByCompanyId2BankId.json", method = RequestMethod.POST)
    public void getBanksByCompanyId2BankId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = companyFacade.getBanksByCompanyId2BankId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
    }
}
